﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using POSSystem.BLL;

namespace WebUI.EditPages
{
    public partial class EditSupllier : BasePage
    {
        Suppliers supplier;
        int supplierID;
        string mode;

        protected void Page_Load(object sender, EventArgs e)
        {
            base.Page_Load(sender, e);

            if (Request.QueryString[KEY] == null)
            {
                CloseDlg(0);
                return;
            }

            if (Request.QueryString[MODE] == null)
            {
                CloseDlg(0);
                return;
            }

            mode = Request.QueryString[MODE];

            if (Request.QueryString[ID] == null && (mode == EDIT_MODE || mode == VIEW_MODE))
            {
                CloseDlg(0);
                return;
            }

            supplier = (Suppliers)Session[Request.QueryString[KEY]];
            string oldFilter = supplier.Filter;

            supplierID = Request.QueryString[ID] != null ? int.Parse(Request.QueryString[ID]) : 0;
            if (mode != ADD_MODE)
                supplier.Filter = Customers.ColumnNames.ID + "=" + supplierID.ToString();
            else
                btnSave.Enabled = false;

            if (!IsPostBack && supplier.RowCount > 0 && mode != ADD_MODE)
                LoadInfo();

            supplier.Filter = oldFilter;
        }

        public void btnSave_OnClick(Object sender, EventArgs e)
        {
            Save();
            ScriptManager.RegisterClientScriptBlock(this, typeof(Page), Guid.NewGuid().ToString(), String.Format("window.returnValue={0};", 1), true);
        }

        private bool Save()
        {
            string oldFilter = supplier.Filter;
            try
            {
                if (mode == ADD_MODE)
                {
                    supplier.AddNew();
                    supplier.BranchID = Branches.CurrentBranchID;
                    supplier.Deleted = false;
                    supplier.TotalAmount = 0;
                    supplier.PaidAmount = 0;
                }
                else
                {
                    supplier.Filter = Customers.ColumnNames.ID + "=" + supplierID;
                }

                supplier.LastModifiedByUser = SessionUser.ID;
                supplier.LastModifiedDate = DateTime.UtcNow.AddHours(SysVars.TimeZone);
                supplier.SupplierName = editSupplier.SupplierName;
                supplier.Address = editSupplier.Address;
                supplier.Phone = editSupplier.Phone;
                supplier.Email = editSupplier.Email;
                supplier.ABN = editSupplier.ABN;
                supplier.AccountNo = editSupplier.AccountNo;
                supplier.Fax = editSupplier.Fax;

                supplier.Save();
                supplier.Filter = oldFilter;
                ((Popup)Master).ShowMessage("Saved");
                return true;
            }
            catch (Exception ex)
            {
                supplier.RejectChanges();
                supplier.Filter = oldFilter;

                ((Popup)Master).ShowErrorMessage(ex.Message);
                ErrorLog.WriteToFile("editSupplier.aspx.cs", "Save", ex.StackTrace);
                return false;
            }
        }

        private void LoadInfo()
        {
            try
            {
                if (!supplier.IsColumnNull(Suppliers.ColumnNames.SupplierName))
                    editSupplier.SupplierName = supplier.SupplierName;
                if (!supplier.IsColumnNull(Suppliers.ColumnNames.Address))
                    editSupplier.Address = supplier.Address;
                if (!supplier.IsColumnNull(Suppliers.ColumnNames.ABN))
                    editSupplier.ABN = supplier.ABN;
                if (!supplier.IsColumnNull(Suppliers.ColumnNames.Phone))
                    editSupplier.Phone = supplier.Phone;
                if (!supplier.IsColumnNull(Suppliers.ColumnNames.AccountNo))
                    editSupplier.AccountNo = supplier.AccountNo;
                if (!supplier.IsColumnNull(Suppliers.ColumnNames.Fax))
                    editSupplier.Fax = supplier.Fax;
                if (!supplier.IsColumnNull(Suppliers.ColumnNames.Email))
                    editSupplier.Email = supplier.Email;
            }
            catch (Exception ex)
            {
                ((Popup)Master).ShowErrorMessage(ex.Message);
                ErrorLog.WriteToFile("editSupplier.aspx.cs", "LoadInfo", ex.StackTrace);
            }
        }

        public void btnSaveAndClose_OnClick(Object sender, EventArgs e)
        {
            if (Save())
                CloseDlg(1);
        }
    }
}
